//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_testcase"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
)

func processTestCaseEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		createCaseEvent := &events_testcase.CreateCaseEvent{}
		if event.EventData.MessageIs(createCaseEvent) {
			err = event.EventData.UnmarshalTo(createCaseEvent)
			if err != nil {
				return false, err
			}

			msg = fmt.Sprintf("%s 在项目 %s 创建测试用例 %s。", event.UserDisplayName, event.ProjectName,
				createCaseEvent.CaseTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.TestcaseEvCfg.CreateCase {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}
	{
		updateCaseEvent := &events_testcase.UpdateCaseEvent{}
		if event.EventData.MessageIs(updateCaseEvent) {
			err = event.EventData.UnmarshalTo(updateCaseEvent)
			if err != nil {
				return false, err
			}

			msg = fmt.Sprintf("%s 在项目 %s 更新测试用例 %s (原标题 %s)。", event.UserDisplayName, event.ProjectName,
				updateCaseEvent.NewCaseTitle, updateCaseEvent.OldCaseTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.TestcaseEvCfg.UpdateCase {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}
	{
		removeCaseEvent := &events_testcase.RemoveCaseEvent{}
		if event.EventData.MessageIs(removeCaseEvent) {
			err = event.EventData.UnmarshalTo(removeCaseEvent)
			if err != nil {
				return false, err
			}

			msg = fmt.Sprintf("%s 在项目 %s 把测试用例 %s 移入回收站", event.UserDisplayName, event.ProjectName,
				removeCaseEvent.CaseTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.TestcaseEvCfg.RemoveCase {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}
	{
		linkSpritEvent := &events_testcase.LinkSpritEvent{}
		if event.EventData.MessageIs(linkSpritEvent) {
			err = event.EventData.UnmarshalTo(linkSpritEvent)
			if err != nil {
				return false, err
			}

			msg = fmt.Sprintf("%s 在项目 %s 关联测试用例 %s 和 工作计划 %s。", event.UserDisplayName, event.ProjectName,
				linkSpritEvent.CaseTitle, linkSpritEvent.SpritId)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.TestcaseEvCfg.LinkSprit {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}
	{
		unlinkSpritEvent := &events_testcase.UnlinkSpritEvent{}
		if event.EventData.MessageIs(unlinkSpritEvent) {
			err = event.EventData.UnmarshalTo(unlinkSpritEvent)
			if err != nil {
				return false, err
			}

			msg = fmt.Sprintf("%s 在项目 %s 取消关联测试用例 %s 和 工作计划 %s。", event.UserDisplayName, event.ProjectName,
				unlinkSpritEvent.CaseTitle, unlinkSpritEvent.SpritId)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.TestcaseEvCfg.UnlinkSprit {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
